<template>
  <j-modal
    :maskClosable="false"
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="打印模版" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-search-select-tag type="list" v-decorator="['printTemplateId', validatorRules.printTemplateId]"
                               :trigger-change="true" dict="sys_sms_template,template_name,id,template_code='houses_rental_contract'"
                               placeholder="请选择打印模版"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

import { updatePrintTemplateId } from '@/api/leaseContract'
import JSearchSelectTag from '@comp/dict/JSearchSelectTag'

export default {
  name: 'LeaseContractSelectTemplateModal',
  components: {
    JSearchSelectTag,
  },
  data () {
    return {
      form: this.$form.createForm(this),
      title: '选择打印模版',
      width: 600,
      visible: false,
      model: {},
      type: 0,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        printTemplateId: {
          rules: [
            { required: true, message: '请选择打印模版!' }
          ]
        },
      }
    }
  },
  created () {

    // this.selectSub()
  },
  methods: {
    show (re, type) {
      this.confirmLoading = false
      this.form.resetFields()
      this.type = type
      this.model = Object.assign({}, re)
      this.visible = true
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          updatePrintTemplateId(formData).then((res) => {
            if (res.success) {
              // this.$message.success(res.message)
              this.$emit('ok', that.model, that.type)
              this.close()
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }

      })
    },
    handleCancel () {
      this.close()
    }

  }
}
</script>